# -*- coding: utf-8 -*-
# Created by bussanq on 2019/06/20.
from bqtool.ai.bqkeras import utils

__author__ = 'bussanq'

import tensorflow as tf
import jieba as jb
import numpy as np

titles,like_len,nplike_len = utils.load_data(catalogue=utils.BINARY_FLAG)
target = utils.load_label(like_len,nplike_len,catalogue=utils.BINARY_FLAG)

max_sequence_length = 30
embedding_size = 50

# 标题分词
titles = [".".join(jb.cut(t, cut_all=True)) for t in titles]

# word2vec 词袋化
vocab_processor = tf.contrib.learn.preprocessing.VocabularyProcessor(max_sequence_length, min_frequency=1)
text_processed = np.array(list(vocab_processor.fit_transform(titles)))

# 读取标签
dict = vocab_processor.vocabulary_._mapping
sorted_vocab = sorted(dict.items(), key = lambda x : x[1])

# 配置网络结构
model = utils.build_netword(catalogue=utils.BINARY_FLAG, dict=dict, embedding_size=embedding_size, max_sequence_length=max_sequence_length)

# 训练模型
# model.fit(text_processed, target, batch_size=512, epochs=50, )
# 保存模型
# model.save("health_and_tech.h5")

# 加载预训练的模型
model.load_weights("health_and_tech.h5")

# 预测样本
sen = "美国16岁少年在Xbox线下二手交易中被枪杀"
sen_prosessed = " ".join(jb.cut(sen, cut_all=True))
sen_prosessed = vocab_processor.transform([sen_prosessed])
sen_prosessed = np.array(list(sen_prosessed))
result = model.predict(sen_prosessed)
print(result)
if result < 0.5:
    print("这是一篇喜欢的文章")
else:
    print("这是一篇不喜欢的文章")